package com.colortea.action.backstage.authority;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.colortea.action.backstage.base.BaseTAction;
import com.colortea.action.backstage.utils.BaseTools;
import com.colortea.action.backstage.utils.statickey.StaticKey;
import com.colortea.entity.FunctionM;
import com.colortea.service.FunctionTService;
import com.colortea.service.impl.Serial;
@Namespace("")
@ParentPackage("colortea")
public class FunctionTAction extends BaseTAction {
	private static final long serialVersionUID = 1L;
	private FunctionTService functionTService;
	private String id;
    private String visiturl;
    private String visitmethodname;
    private String functionname;
    private Date createtime;
    private String moduleid;
    private FunctionM bean=new FunctionM();
    private List<FunctionM> beanlist=new ArrayList<FunctionM>();
	private Map<String,Object>map=new HashMap<String,Object>();
	private List<Map<String, Object>> rows=new ArrayList<Map<String, Object>>();
	private int rp;
	private int page = 1;
	private int total = 0;
	private boolean sucflag;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getVisiturl() {
		return visiturl;
	}
	public void setVisiturl(String visiturl) {
		this.visiturl = visiturl;
	}
	public String getVisitmethodname() {
		return visitmethodname;
	}
	public void setVisitmethodname(String visitmethodname) {
		this.visitmethodname = visitmethodname;
	}
	public String getFunctionname() {
		return functionname;
	}
	public void setFunctionname(String functionname) {
		this.functionname = functionname;
	}
	public Date getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	public String getModuleid() {
		return moduleid;
	}
	public void setModuleid(String moduleid) {
		this.moduleid = moduleid;
	}

	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	public List<Map<String, Object>> getRows() {
		return rows;
	}
	public void setRows(List<Map<String, Object>> rows) {
		this.rows = rows;
	}
	public int getRp() {
		return rp;
	}
	public void setRp(int rp) {
		this.rp = rp;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public boolean isSucflag() {
		return sucflag;
	}
	public void setSucflag(boolean sucflag) {
		this.sucflag = sucflag;
	}
	
	/**
	 * 清理错误
	 */
	@Override
	public void validate() {
		this.clearErrorsAndMessages();

	}
	
	/**
	 * 增加可访问的功能路径或方法
	 * @return
	 */
	@Action(value = "addFunctionT", results = { @Result(name = "json", type = "json") })
	public String addFunctionT(){
		FunctionM fm=new FunctionM();
		fm.setId(this.getSerial().Serialid(Serial.FUNCTION));
		fm.setFunctionname(this.getFunctionname());
		fm.setVisitmethodname(this.getVisitmethodname());
		fm.setVisiturl(this.getVisiturl());
		fm.setModuleid(this.getModuleid());
		fm.setCreatetime(BaseTools.getSystemTime());
		this.functionTService.save(fm);
		this.setSucflag(true);
		return "json";
	}
	
	/**
	 * 根据id获取功能信息数据
	 * @return
	 */
	@Action(value = "findFunctionTById", results = { @Result(name = "json", type = "json") })
	public String findFunctionTById(){
		if(StringUtils.isNotBlank(this.getId())){
			bean=this.functionTService.findByPK(FunctionM.class, this.getId());
			if(bean!=null){
				this.setSucflag(true);
				return "json";
			}
			this.setSucflag(false);
			return "json";
		}
		this.setSucflag(false);
		return "json";
	}
	
	/**
	 * 更新功能
	 * @return
	 */
	@Action(value = "updateFunctionT", results = { @Result(name = "json", type = "json") })
	public String updateFunctionT(){
		if(StringUtils.isNotBlank(this.getId())){
			bean=this.functionTService.findByPK(FunctionM.class, this.getId());
		}
		bean.setId(this.getId());
		bean.setFunctionname(this.getFunctionname());
		bean.setVisitmethodname(this.getVisitmethodname());
		bean.setVisiturl(this.getVisiturl());
		bean.setModuleid(this.getModuleid());
		bean.setCreatetime(BaseTools.getSystemTime());
		this.functionTService.update(bean);
		this.setSucflag(true);
		return "json";
	}
	
	/**
	 * 根据模块id查询对应功能列表
	 * @return
	 */
	@Action(value = "findAllFunctionTBymoduleid", results = { @Result(name = "json", type = "json") })
	public String findAllFunctionTBymoduleid(){
		if(StringUtils.isNotBlank(this.getModuleid())){
			this.findDefaultAllFunctionTBymoduleid();
		}else{
			this.findAllFunctionT();
		}
		return "json";
	}
	
	
	public void findDefaultAllFunctionTBymoduleid(){
		Criterion criterion=Restrictions.eq("moduleid", this.getModuleid());
		List<FunctionM>list=this.functionTService.findByCriteria(FunctionM.class, criterion);
		if(!list.isEmpty()){
			total=list.size();
			this.ProcessFunctionTList(list);
		}
	}
	
	public void findAllFunctionT(){
		List<FunctionM>list=this.functionTService.findAll(FunctionM.class);
		if(!list.isEmpty()){
			total=list.size();
			this.ProcessFunctionTList(list);
		}
	}
	
	public void ProcessFunctionTList(List<FunctionM>list){
		for(Iterator<FunctionM> it=list.iterator();it.hasNext();){
			FunctionM fm=(FunctionM)it.next();
			Map<String,Object>cellMap=new HashMap<String,Object>();
			cellMap.put("id", fm.getId());
			cellMap.put("cell", new Object[]{
				fm.getFunctionname(),
				fm.getVisiturl(),
				fm.getVisitmethodname(),
				BaseTools.formateDbDate(fm.getCreatetime()),
				"<a id='editfunction' name='editfunction' href='function.jsp?operate=edit&folder=setting&id="+fm.getId()+"'>[编辑]</a>"
			});
			rows.add(cellMap);
		}
	}
	
	/**
	 * 删除系统模块功能
	 * @return
	 */
	@Action(value = "delFunctionT", results = { @Result(name = "json", type = "json") })
	public String delFunctionT(){
		if(StringUtils.isNotBlank(this.getId())){
			String []strs=StringUtils.split(this.getId(),StaticKey.SPLITDOT);
			for(String s:strs){
				bean=this.functionTService.findByPK(FunctionM.class, s);
				if(bean!=null){
					this.functionTService.delete(bean);
				}
			}
			this.setSucflag(false);
			return "json";
		}
		this.setSucflag(false);
		return "json";
	}
	
	/**
	 * 获取所有功能，给前台的checkbox显示
	 * @return
	 */
	@Action(value = "findAllFunction", results = { @Result(name = "json", type = "json") })
	public String findAllFunction(){
		beanlist=this.functionTService.findAll(FunctionM.class);
		return "json";
	}
	
}
